﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for UnhandledException.xaml
    /// </summary>
    public partial class UnhandledException : Window
    {
        public UnhandledException()
        {
            this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(UnhandledException_Loaded);
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            this.ShowInTaskbar = false;
            this.Topmost = true;
            // Insert code required on object creation below this point.
        }

        void UnhandledException_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
            dt.Interval = TimeSpan.FromSeconds(1);
            dt.Tick += new EventHandler(dt_Tick);
            dt.IsEnabled = true;
            try
            {
                Height = (this.Parent as Window).ActualHeight;
                Width = (this.Parent as Window).ActualWidth;
            }
            catch
            {
            }
        }
        int count = 30;
        void dt_Tick(object sender, EventArgs e)
        {
            count--;
            CountDown.Text = (count.ToString());
            if (count <= 2)
            {
                CountDown.Text = "";
                CountdownContent.Text = "restarting...";
            }
            if (count == 0)
            {
                Application.Current.Shutdown();
                System.Diagnostics.Process.Start(System.Reflection.Assembly.GetEntryAssembly().Location);
            }

        }
        public Exception ex;
        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;
            }
            var body = "mailto:llmainland@live.cn?subject=ExceptionDispatch&body=";
            var format = string.Format("SOURCE:%0A{0}%0A%0AMESSAGE:%0A{1}%0A%0ASTACKTRACE:%0A {2} %0AWould you be kindly enough to tell us how to reproduce this problem:", ex.Source, ex.Message, ex.StackTrace.Replace("\r\n", "%0A"));
            System.Diagnostics.Process.Start(body + format);
        }
    }
}